home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / Apache 1.0 / support / unescape.c < prev   
C/C++ Source or Header  |  1995-12-04  |  2KB  |  92 lines

  1. /*
  2.  * Short program to unescape a URL string
  3.  * 
  4.  * Rob McCool
  5.  * 
  6.  */
  7.  
  8. #include <stdio.h>
  9.  
  10. void plustospace(char *str) {
  11.     register int x;
  12.  
  13.     for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
  14. }
  15.  
  16. char x2c(char *what) {
  17.     register char digit;
  18.  
  19.     digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
  20.     digit *= 16;
  21.     digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
  22.     return(digit);
  23. }
  24.  
  25. void unescape_url(char *url) {
  26.     register int x,y;
  27.  
  28.     for(x=0;url[x];x++)
  29.         if(url[x] == '%')
  30.             url[x+1] = x2c(&url[x+1]);
  31.  
  32.     for(x=0,y=0;url[y];++x,++y) {
  33.         if((url[x] = url[y]) == '%') {
  34.             url[x] = url[y+1];
  35.             y+=2;
  36.     }
  37.     }
  38.     url[x] = '\0';
  39. }
  40.  
  41. int ind(char *s, char c) {
  42.     register int x;
  43.  
  44.     for(x=0;s[x];x++)
  45.         if(s[x] == c) return x;
  46.  
  47.     return -1;
  48. }
  49.  
  50. void escape_shell_cmd(char *cmd) {
  51.     register int x,y,l;
  52.  
  53.     l=strlen(cmd);
  54.     for(x=0;cmd[x];x++) {
  55.         if(ind("&;`'|*?-~<>^()[]{}$\\",cmd[x]) != -1){
  56.             for(y=l+1;y>x;y--)
  57.                 cmd[y] = cmd[y-1];
  58.             l++; /* length has been increased */
  59.             cmd[x] = '\\';
  60.             x++; /* skip the character */
  61.         }
  62.     }
  63. }
  64.  
  65. void usage(char *name) {
  66.     fprintf(stderr,"Usage:\n%s [-e] url\n",name);
  67.     fprintf(stderr,
  68. "The -e switch automatically escapes shell characters like ^ and &\n");
  69.     fprintf(stderr,"and url is the encoded url string\n");
  70.     exit(1);
  71. }
  72.  
  73. main(int argc, char *argv[]) {
  74.  
  75.     if((argc != 2) && (argc != 3))
  76.         usage(argv[0]);
  77.     else {
  78.         char *t;
  79.  
  80.         t = (char *) malloc(sizeof(char) * (strlen(argv[argc-1])+1));
  81.         strcpy(t,argv[argc-1]);
  82.         plustospace(t);
  83.         unescape_url(t);
  84.         if(argc == 3) {
  85.             if(strcmp(argv[1],"-e"))
  86.                 usage(argv[0]);
  87.             escape_shell_cmd(t);
  88.         }
  89.         printf("%s",t);
  90.     }
  91. }
  92.